package tr.edu.yildiz.ce.app_manager.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.SequenceGenerator;

@Entity(name="APP_PROXY")
public class AppProxy extends BasicObject<Long>{
	
	@Id
	@SequenceGenerator(name = "proxy_generator", sequenceName = "SEQ_PROXY", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "proxy_generator")
    private Long proxyId;
	
	@Column(name = "PROXY_NAME", nullable = false, length = 100)
	private String proxyName; 
	
	@Column(name = "PROXY_IP", nullable = false, length = 15)
	private String proxyIp;

	@Column(name = "PROXY_USERNAME", nullable = false, length = 50)
	private String proxyUsername;

	@Column(name = "PROXY_PASSWORD", nullable = false, length = 10)
	private String proxyPassword;
	
	@OneToOne(fetch=FetchType.EAGER)
	@JoinColumn(name = "PARENT_ID")
	private AppProxy parent;
	
	@ManyToMany( cascade=CascadeType.REFRESH,fetch=FetchType.EAGER)
	@JoinTable(name = "PROXY_APPLICATION",
	joinColumns = {@JoinColumn(name="PROXY_ID")},inverseJoinColumns={@JoinColumn(name="APPLICATION_ID")})
	private Set<Application> applications;
	
	public Long getProxyId() {
		return proxyId;
	}
	
	public void setProxyId(Long proxyId) {
		this.proxyId = proxyId;
	}
	
	public String getProxyName() {
		return proxyName;
	}
	
	public void setProxyName(String proxyName) {
		this.proxyName = proxyName;
	}

	public void setParent(AppProxy parent) {
		this.parent = parent;
	}

	public AppProxy getParent() {
		return parent;
	}

	public void setProxyIp(String proxyIp) {
		this.proxyIp = proxyIp;
	}

	public String getProxyIp() {
		return proxyIp;
	}
	
	public Set<Application> getApplications() {
		return applications;
	}
	
	public void setApplications(Set<Application> applications) {
		this.applications = applications;
	}
	
	public String getProxyUsername() {
		return proxyUsername;
	}

	public void setProxyUsername(String proxyUsername) {
		this.proxyUsername = proxyUsername;
	}

	public String getProxyPassword() {
		return proxyPassword;
	}

	public void setProxyPassword(String proxyPassword) {
		this.proxyPassword = proxyPassword;
	}

	@Override
	public String toString() {
		return proxyName + "[" + proxyIp + "]";
	}
	
	@Override
	public boolean equals(Object obj) {
		if (obj == null)
			return false;
		AppProxy appProxy = (AppProxy) obj;
		return appProxy.getId().equals(this.getId());
	}

	@Override
	public Long getId() {
		return proxyId;
	}
	
}
